/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package backupit;

import backupit.core.BackupList;
import backupit.core.io.LoadBackupList;
import backupit.core.io.LoadReportQueue;
import backupit.core.io.SaveBackupList;
import backupit.core.io.SaveReportQueue;
import backupit.core.report.ReportManager;
import backupit.core.report.ReportQueue;
import java.io.File;
import java.io.FileNotFoundException;

/**
 *
 * @author dbotelho
 */
public class Settings {
    

    public static String ROOT_DIR = System.getProperty("user.dir") + File.separatorChar + ".backupit";
    public static String BACKUPLIST_EXTENSION =".bkit";
    public static String BACKUPLIST_FILENAME = "backuplist"+BACKUPLIST_EXTENSION;
    public static String REPORTMANAGER_EXTENSION=".queue";
    public static String REPORTMANAGER_FILENAME = "report"+REPORTMANAGER_EXTENSION;
    
    private static Settings settings = null;
    
    private static void createRootDir() {
        new File(ROOT_DIR).mkdir();
    }
    
    public ReportQueue loadReportQueue() throws FileNotFoundException, InterruptedException {
        return loadReportQueue(ROOT_DIR + File.separatorChar + REPORTMANAGER_FILENAME);
    }
    
    public ReportQueue loadReportQueue(String fileName) throws FileNotFoundException, InterruptedException {
        LoadReportQueue loader = new LoadReportQueue(fileName);
        Thread t = new Thread(loader);
        t.start();
        t.join();
        return loader.getReportQueue();
    }
    
    public void saveReportQueue(String fileName,ReportQueue reportQueue) throws FileNotFoundException, InterruptedException {
        Thread t =new Thread(new SaveReportQueue(
                fileName, reportQueue));
        t.start();
        t.join();
    }

    public void saveReportQueue(ReportQueue reportQueue) throws FileNotFoundException, InterruptedException {
        saveReportQueue(
                ROOT_DIR + File.separatorChar + REPORTMANAGER_FILENAME, reportQueue);
    }
    
    public BackupList loadBackupList() throws FileNotFoundException, InterruptedException {
        return loadBackupList(ROOT_DIR + File.separatorChar + BACKUPLIST_FILENAME);
    }
    
    public BackupList loadBackupList(String fileName) throws FileNotFoundException, InterruptedException {
        LoadBackupList loader = new LoadBackupList(fileName);
        Thread t = new Thread(loader);
        t.start();
        t.join();
        return loader.getBackupList();
    }
    
    public void saveBackupList(String fileName,BackupList backuplist) throws FileNotFoundException, InterruptedException {
        Thread t =new Thread(new SaveBackupList(
                fileName, backuplist));
        t.start();
        t.join();
    }

    public void saveBackupList(BackupList backuplist) throws FileNotFoundException, InterruptedException {
        saveBackupList(
                ROOT_DIR + File.separatorChar + BACKUPLIST_FILENAME, backuplist);
    }
    
    public static Settings getInstance() throws FileNotFoundException {
        if (settings == null) {
            settings = new Settings();

            createRootDir();

        }
        return settings;
    }
    
    
}
